package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcDictionaryMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcDictypeEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.busi.service.ICdcDictypeService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 业务字典表数据服务层接口
 * 
 * @ClassName: CdcDictionaryServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Service("cdcDictionaryService")
public class CdcDictionaryServiceImpl extends BaseServiceImpl<CdcDictionaryMapper, CdcDictionaryEntity> implements ICdcDictionaryService {

    @Autowired
    ICdcDictypeService dicTypeSrv;

    @Override
	public List<CdcDictionaryEntity> page(Page<CdcDictionaryEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcDictionaryEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}
	@Override
    public List<CdcDictionaryEntity> queryByTypeCode(String dataCode, String typeCode) {
        List<CdcDictionaryEntity> result = null;
        CdcDictypeEntity dicType = dicTypeSrv.queryByCode(dataCode, typeCode).get(0);
        if(dicType != null){
            String dictypeId= String.valueOf(dicType.getId());
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("dictypeId", dictypeId);
            result = this.baseMapper.list(map);
        }

        return result;
    }

    @Override
    public CdcDictionaryEntity queryByTypeCodeAndCode(String dataCode, String dicTypeCode, String typeCode) {
        CdcDictionaryEntity dictionaryEntity = null;
        List<CdcDictionaryEntity> cdcDictionaryEntities = this.queryByTypeCode(dataCode, dicTypeCode);
        for (CdcDictionaryEntity cdcDictionaryEntity : cdcDictionaryEntities){
            if (Objects.equals(typeCode, cdcDictionaryEntity.getCode())){
                dictionaryEntity = cdcDictionaryEntity;
            }
        }
        return dictionaryEntity;
    }

}